
const ac_cgi_url = '/cgi-bin/luci/admin/apps-index/ac-server/api/ac-server.lua';
const http_request = async (url: string, values: any, method = 'POST', customHeaders: any = {}) => {
    const defaultContentType = method.toUpperCase() === 'GET' ? 'application/x-www-form-urlencoded' : 'application/json';
    
    const headers = {
        ...customHeaders,
        'Content-Type': customHeaders['Content-Type'] || defaultContentType
    };

    try {
        const response = await fetch(url, {
            method: method.toUpperCase(),
            headers,
            body: method.toUpperCase() === 'GET' ? null : JSON.stringify(values)
        });

        if (!response.ok) {
            throw new Error(response.statusText);
        }

        const responseText = await response.text();
        const responseObject = JSON.parse(responseText);

        return responseObject;
    } catch (error: any) {
        throw new Error(error.message || "An unexpected error occurred.");
    }
};

export {
    http_request,
    ac_cgi_url
};